<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px" label-position="left">
      <el-row>
        <el-col :span="6">
          <el-form-item label="商家名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入商家名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="联系人姓名" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="联系人手机号" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入手机号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="代理商名称" prop="agentName">
            <el-input
              v-model="queryParams.agentName"
              placeholder="请输入代理商名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="商家状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="全部" clearable>
              <el-option
                v-for="dict in dict.type.sys_agent"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="queryParams.email"
              placeholder="请输入邮箱"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="button-right buttons">
      <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
      <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
    </div>
    <el-divider></el-divider>
    <div class="header-box">
      <div class="address header-left">
        <span class="span-text">
          商户登录地址：
        </span>
        <span class="span-text website-blue">
          {{ address }}
        </span>
        <el-button
          type="primary"
          icon="el-icon-document-copy"
          size="small"
          @click="onCopy"
        >复制</el-button>
      </div>

      <div class="button-right">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['store:storeManage:add']"
        >新增</el-button>
      </div>
    </div>
    <el-table v-loading="loading" :data="storeManageList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="55" align="center" />
<!--      <el-table-column label="商家主键ID" align="center" prop="id" />-->
      <el-table-column label="商家名称" align="center" prop="name" />
<!--      <el-table-column label="所属代理商id" align="center" prop="agentId" />-->

      <el-table-column label="所属代理商名称" align="center" prop="agentName" />
      <el-table-column label="联系人名称" align="center" prop="userName" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="联系人邮箱" align="center" prop="email" />

<!--      <el-table-column label="所在城市" align="center" prop="city" />-->
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="余额" align="center" prop="balance" />
<!--      <el-table-column label="标识" align="center" prop="mark" />-->
<!--      <el-table-column label="密码" align="center" prop="password" />-->
<!--      <el-table-column label="0线下 1 短信/邮件授权" align="center" prop="authMethod">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_store_authomethod" :value="scope.row.authMethod"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="认证权限 0允许  1不允许" align="center" prop="authStatus">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_store_autostatus" :value="scope.row.authStatus"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="帐号状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeStatus(scope.row)">
          </el-switch>
          <!-- <dict-tag :options="dict.type.sys_agent" :value="scope.row.status"/> -->
        </template>
      </el-table-column>
<!--      <el-table-column label="套餐id" align="center" prop="packageId" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:storeManage:edit']"
            style="color: #017EFF;"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:storeManage:remove']"
            style="color: #017EFF;"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改管理对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>


    </el-dialog> -->
  </div>
</template>

<script>
import { listStoreManage, getStoreManage, delStoreManage, addStoreManage, updateStoreManage, resetStatus, getStoreAddress } from "@/api/store/storeManage";

export default {
  name: "StoreManage",
  dicts: ['sys_store_authomethod', 'sys_store_autostatus', 'sys_agent'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管理表格数据
      storeManageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        agentId: null,
        agentName: null,
        userName: null,
        email: null,
        phone: null,
        city: null,
        address: null,
        authMethod: null,
        authStatus: null,
        status: null,
      },

      // 商户登录地址
      address: 'www.xxxx.com',
      detailData: {
        clientSecret: 'your-secret-key-here' // 确保这里有一个初始值
      },
    };
  },
  created() {
    this.getList();
    this.getAddress();
  },
  methods: {
    // 获取商户网址
    getAddress() {
      getStoreAddress().then(res => {
        this.address = res.data;
      })
    },
    /** 查询管理列表 */
    getList() {
      this.loading = true;
      listStoreManage(this.queryParams).then(response => {
        this.storeManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        agentId: null,
        agentName: null,
        userName: null,
        email: null,
        phone: null,
        city: null,
        address: null,
        balance: null,
        mark: null,
        password: null,
        authMethod: null,
        authStatus: null,
        status: null,
        packageId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/storeManage/addStore', query: { type: 'add' } });
      // this.reset();
      // this.open = true;
      // this.title = "添加管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/storeManage/addStore', query: { type: 'edit', id: row.id || this.ids } });
      // this.reset();
      // const id = row.id || this.ids
      // getStoreManage(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改管理";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStoreManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除管理编号为"' + ids + '"的数据项？').then(function() {
        return delStoreManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/storeManage/export', {
        ...this.queryParams
      }, `storeManage_${new Date().getTime()}.xlsx`)
    },
    // 复制
    onCopy() {
      navigator.clipboard.writeText(this.address).then(() => {
        this.$message.success('复制成功')
      })
    },
    // 状态开关
    changeStatus(row) {
      let params = {};
      params.storeId = row.id;
      params.status = row.status;
      resetStatus(params).then(response => {
        this.getList();
      });
    },
  }
};
</script>

<style lang="scss" scoped>
  .app-container {
    .address {
      margin-left: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      font-size: 14px;
      color: #333333;
      display: flex;
      .el-button {
        margin-left: 21px;
      }
      .span-text {
        transform: translateY(5px);
      }
      .website-blue {
        color: #535E83;
      }
    }
    .button-right {
      margin-right: 55px;
    }
    .buttons {
      float: right;
      transform: translateY(-28px);
    }
  }
</style>
